import axios from 'axios'

// 高德地图驾车路径规划响应类型
interface GaoDeRouteResponse {
  status: string
  info: string
  infocode: string
  count: string
  route: {
    origin: string
    destination: string
    paths: Path[]
  }
}

interface Path {
  distance: string
  duration: string
  strategy: string
  tolls: string
  toll_distance: string
  steps: Step[]
  restriction: string
  traffic_lights: string
}

export interface Step {
  instruction: string
  orientation: string
  distance: string
  tolls: string
  toll_distance: string
  toll_road: never[] // 可根据实际数据结构进一步细化
  duration: string
  polyline: string
  action: string
  assistant_action: string
}

export async function getGaoDeRoute(start: string, end: string): Promise<GaoDeRouteResponse> {
  try {
    const response = await axios.get(`${GAODE_API_CONFIG.baseUrl}/direction/driving`, {
      params: {
        key: GAODE_API_CONFIG.key,
        origin: start,
        destination: end,
        extensions: 'all',
        output: 'json',
      },
    })
    return response.data
  } catch (error) {
    console.error('高德地图API请求失败:', error)
    throw error
  }
}
// 高德地图 API 配置
const GAODE_API_CONFIG = {
  key: '7f7a58932a276710392d574317aacdf4',
  baseUrl: 'https://restapi.amap.com/v3',
}

// POI 信息接口
export interface PoiItem {
  id: string
  name: string
  type: string
  typecode: string
  address: string
  location: string
  distance: string
  tel: string
  pname: string
  cityname: string
  adname: string
  business_area: string
  [key: string]: any // 其他可能的字段
}

// 周边搜索响应接口
export interface AroundSearchResponse {
  status: string
  info: string
  infocode: string
  count: string
  pois: PoiItem[]
  suggestion?: any
}

/**
 * 搜索周边 POI
 * @param location 中心点坐标，格式：'longitude,latitude'
 * @param types POI 类型代码
 * @param radius 搜索半径，单位：米
 * @param keywords 关键字
 * @param page 页码
 * @param offset 每页记录数
 * @returns Promise<AroundSearchResponse>
 */
export async function searchAround(
  location: string,
  types: string,
  radius: number = 1000,
  keywords: string = '',
  page: number = 1,
  offset: number = 20,
): Promise<AroundSearchResponse> {
  try {
    const response = await axios.get(`${GAODE_API_CONFIG.baseUrl}/place/around`, {
      params: {
        key: GAODE_API_CONFIG.key,
        location,
        types,
        radius,
        keywords,
        offset,
        page,
        extensions: 'all',
      },
    })

    return response.data
  } catch (error) {
    console.error('高德地图 API 请求失败:', error)
    throw error
  }
}

/**
 * 根据经纬度坐标获取周边指定类型的 POI 列表
 * @param longitude 经度
 * @param latitude 纬度
 * @param poiType POI 类型代码
 * @param radius 搜索半径，单位：米
 * @returns Promise<PoiItem[]>
 */
export async function getPoiListByLocation(
  longitude: number,
  latitude: number,
  poiType: string,
  radius: number = 1000,
): Promise<PoiItem[]> {
  const location = `${longitude},${latitude}`
  const result = await searchAround(location, poiType, radius)

  if (result.status === '1') {
    return result.pois
  } else {
    throw new Error(`获取 POI 失败: ${result.info}`)
  }
}

/**
 * 获取地铁站周边设施
 * @param longitude 地铁站经度
 * @param latitude 地铁站纬度
 * @param facilityType 设施类型代码，例如：'050301'(餐饮),'011100'(充电站)等
 * @param radius 搜索半径，单位：米
 * @returns Promise<PoiItem[]>
 */
export async function getSubwayStationSurroundingFacilities(
  longitude: number,
  latitude: number,
  facilityType: string,
  radius: number = 1000,
): Promise<PoiItem[]> {
  return getPoiListByLocation(longitude, latitude, facilityType, radius)
}

// 常用 POI 类型代码
export const POI_TYPE = {
  RESTAURANT: '050000', // 餐饮
  SHOPPING: '060000', // 购物
  LIFE_SERVICE: '070000', // 生活服务
  ENTERTAINMENT: '080000', // 休闲娱乐
  MEDICAL: '090000', // 医疗
  HOTEL: '100000', // 住宿
  SCENIC_SPOT: '110000', // 风景名胜
  BUSINESS: '120000', // 商务
  TRANSPORTATION: '150000', // 交通设施
  CHARGING_STATION: '011100', // 充电站
  SUBWAY_STATION: '150500', // 地铁站
}
